load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_code.ncl"
load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_csm.ncl" 
load "$NCARG_LIB/ncarg/nclscripts/csm/contributed.ncl"

;;..................................................................................... 
;; nice string 
;;..................................................................................... 
  undef("strnice")
  function strnice(str,nc)
  begin

   string_len = sizeof(stringtochar(str)) - 1

   nstr = str

   if (string_len.lt.nc) then

   do it = 0, nc - string_len - 2
      nstr = nstr + " "
   end do

   end if

  return(nstr)
  end



begin

  expname = "X0022" 
  year    = "2000-2002" 
  expname = "Y0003" 
  year    = "0001-0005"
 
 
  pa = "/pic/scratch/zhan524/"+expname+"/"+expname+".cam2.h0."+year+".ANN.nc" 

  vname  = (/ "CLDLOW", "CLDMED", "CLDHGH", "PRECC", \
              "PRECL", "PRECT", "SWCF", "LWCF", \
              "TCF", "RFP", \
              "FSNT", "FLNT", "FSNTC", "FLNTC", \
              "FSNS", "FLNS", "FSNSC", "FLNSC", \
              "TGCLDLWP", "TGCLDIWP", "AODVIS", "AODABS" /) 

  vprep = 24.*3600.*1000. 
  vlwp  = 1.e3

  vunit = "" 

  vscale = (/ 1., 1., 1., vprep, \ 
              vprep, vprep, 1., 1., \ 
              1., 1., \ 
              1., 1., 1., 1., \ 
              1., 1., 1., 1., \ 
              vlwp, vlwp, 1., 1.  /) 


  caseM  = (/ pa /) 
  case  = (/caseM(0)/) 
  Ncase = dimsizes(case)

  print(" ")
  print(" ")
  print("..................................................................")
  print(" ")
  print(" ")
  print(" exp      : "+case(0))
  print(" ")
  print(" ")

  fngrid = "gridarea.nc"
  fgrid = addfile(fngrid,"r")
  gridarea = fgrid->cell_area
  
  fnmask = "landmask.nc"
  fmask = addfile(fnmask,"r") 
  landmask = fmask->landmask
 
;;......................................................................................
;; Open netCDF file and read in dimensions
;;......................................................................................

   filename=case(0)
   fi=addfile(filename,"r")
   lev   = fi->lev
   lat   = fi->lat
   lon   = fi->lon 
   nlev  = dimsizes(lev)                           
   nlat  = dimsizes(lat)
   nlon  = dimsizes(lon)

;;......................................................................................
;; read in variables & create plot
;;......................................................................................

  do iv=0,dimsizes(vname)-1 

     do i=0,Ncase-1
        filename=case(i)
        fi=addfile(filename,"r")

        if(vname(iv).eq."TCF") then 
           Var3dtema1 = fi->LWCF(time|:,lat|:, lon|:)
           Var3dtema2 = fi->SWCF(time|:,lat|:, lon|:)
           Var3dtema = Var3dtema1 
           Var3dtema = Var3dtema1 + Var3dtema2 
           Var3dtema@long_name = "TCF" 
        end if

        if(vname(iv).eq."RFP") then 
           Var3dtema1 = fi->FSNT(time|:,lat|:, lon|:)
           Var3dtema2 = fi->FLNT(time|:,lat|:, lon|:)
           Var3dtema = Var3dtema1 
           Var3dtema = Var3dtema1 - Var3dtema2
           Var3dtema@long_name = "RFP"  
        end if

        if(vname(iv).ne."RFP" .and. \
           vname(iv).ne."TCF" ) then 
           Var3dtema = fi->$vname(iv)$(time|:,lat|:, lon|:)
        end if   

        Var3dtema=Var3dtema*vscale(iv) 

        Var3dtemp = Var3dtema(0,:,:)
        Var3dtempland = where(landmask.eq.1,Var3dtema(0,:,:),-999) 
        Var3dtempocean = where(landmask.eq.0,Var3dtema(0,:,:),-999) 
        gridarea_land = where(landmask.eq.1,gridarea(:,:),-999) 
        gridarea_ocean = where(landmask.eq.0,gridarea(:,:),-999) 
        Var3dtemp@_FillValue = -999  
        Var3dtempland@_FillValue = -999  
        Var3dtempocean@_FillValue = -999 
        gridarea_land@_FillValue = -999  
        gridarea_ocean@_FillValue = -999 

        global_total = sum(Var3dtemp*gridarea)/sum(gridarea) 
        global_total_land = sum(Var3dtempland*gridarea_land)/sum(gridarea_land) 
        global_total_ocean = sum(Var3dtempocean*gridarea_ocean)/sum(gridarea_ocean) 

        print(strnice(vname(iv),15)+" : "+sprintf("%9.4f", global_total) +"   "+sprintf("%9.4f", global_total_land)+"   "+sprintf("%9.4f", global_total_ocean) ) 

        delete(Var3dtemp)

     end do

  end do
  
end




